package eu.runelord.blog.datecalculator;

import java.util.GregorianCalendar;

public class Calculator {

	private static final GregorianCalendar CALENDAR = new GregorianCalendar(
			1900, 1, 1);

	public static void main(String[] args) {
		GregorianCalendar now = new GregorianCalendar(1900, 0, 1);

		long dayNumber = dayToNumber(now);
		System.out.println(dayNumber);
		GregorianCalendar now2 = numberToDay(dayNumber);
		System.out.println(now2.get(GregorianCalendar.YEAR) + ","
				+ now2.get(GregorianCalendar.MONTH) + ","
				+ now2.get(GregorianCalendar.DAY_OF_MONTH));
	}

	private static GregorianCalendar numberToDay(long now) {
		int year = (int) (now / 365.25);
		int month = (int) ((now + 1 - (year * 365.25)) / 30.4375);
		int day = (int) (1 + (now + 1 - (year * 365.25)) / 12);
		GregorianCalendar ret = new GregorianCalendar(year + 1900, month, day);
		return ret;
	}

	private static long dayToNumber(GregorianCalendar now) {
		long years = 1 + (long) (365.25 * (now.get(GregorianCalendar.YEAR) - CALENDAR
				.get(GregorianCalendar.YEAR)));

		long days = now.get(GregorianCalendar.DAY_OF_YEAR) - 1;

		return years + days;
	}
}
